package logic

import (
	"Wei-Xin/models"
	"context"
	"errors"

	"Wei-Xin/weixinrpc/internal/svc"
	"Wei-Xin/weixinrpc/weixinrpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type PerfectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPerfectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PerfectLogic {
	return &PerfectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 完善个人信息
func (l *PerfectLogic) Perfect(in *weixinrpc.PerfectRequest) (*weixinrpc.PerfectResponse, error) {
	// todo: add your logic here and delete this line

	var user models.User
	models.DB.Debug().Where("id = ?", in.UserId).Find(&user)
	if user.ID == 0 {
		return nil, errors.New("当前用户不存在")
	}

	user = models.User{
		Model:     gorm.Model{ID: uint(in.UserId)},
		Account:   in.Account,
		Password:  in.Password,
		HeadImage: in.HeadImage,
		NickName:  in.NickName,
		Address:   in.Address,
	}
	models.DB.Debug().Updates(&user)

	return &weixinrpc.PerfectResponse{
		UserId: int64(user.ID),
	}, nil
}
